<template>
  <view v-if="detailData.goods_name">
    <swiper indicator-dots="true" autoplay="true" interval="1500" :circular="true">
      <swiper-item v-for="(val,i) in detailData.pics" :key="i">
        <image :src="val.pics_big" mode="" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">￥{{detailData.goods_price}}</view>
      <!-- 信息主体区域 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{detailData.goods_name}}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递：免运费</view>
    </view>
    <rich-text style="padding: 50px;" :nodes="detailData.goods_introduce"></rich-text>

    <!-- 底部导航 -->
    <view class="navi">
      <view class="goods-carts">
        <uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed:{
      ...mapGetters('m_cart',['allCount'])
      },
     watch:{
       allCount:{
         handler(newValue){
         const findRes=this.options.find((item)=>item.text==="购物车")
         if(findRes){
           findRes.info=newValue
         }
     },
     immediate:true
     }},
    data() {
      return {
        detailData: {},
        options: [{
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: this.allCount
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]

      };
    },
    onLoad(option) {
      console.log(option);
      this.getList(option.query)
    },
    methods: {
      async getList(id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: id
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        //对数据改造让图片display=block以消除空白的间隙
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g, `<img style="display:block"`).replace(/webp/g, `jpg`)
        this.detailData = res.message
      },
      preview(i) {
        uni.previewImage({
          current: i,
          urls: this.detailData.pics.map(x => x.pics_big)
        })
      },
      // 点击跳转
      onClick(e) {
        console.log(e);
        if (e.content.text == "购物车") {
          uni.switchTab({
            url: "/pages/cart/cart"
          })
        }
      },
      ...mapMutations('m_cart', ['addToCart']),
      buttonClick(e) {
        const goods = {
          goods_id: this.detailData.goods_id, // 商品的Id
          goods_name: this.detailData.goods_name, // 商品的名称
          goods_price: this.detailData.goods_price, // 商品的价格
          goods_count: 1, // 商品的数量
          goods_small_logo: this.detailData.goods_small_logo, // 商品的图片
          goods_state: true // 商品的勾选状态
        }
        if(e.content.text=="加入购物车"){this.addToCart(goods)
        }
        
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 735rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  // 商品信息区域的样式
  .goods-info-box {
    padding: 10px;
    padding-right: 0;

    .price {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        padding-right: 10px;
      }

      // 收藏区域
      .favi {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    // 运费
    .yf {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }

  .navi {
    position: fixed;
    z-index: 100;
    width: 100%;
    left: 0;
    bottom: 0;
  }
</style>
